package com.wanmait.wanmarecruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.wanmarecruit.annotation.ManageLogAnnotation;
import com.wanmait.wanmarecruit.entity.City;
import com.wanmait.wanmarecruit.service.PositionService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.wanmarecruit.mapper.PositionMapper;
import com.wanmait.wanmarecruit.entity.Position;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionService {

    @Resource
    private PositionMapper positionMapper;

    @Override
    public void batchDele(Integer[] positionIds) {
        List<Position> positions = new ArrayList<>();
        for (int i = 0; i < positionIds.length; i++) {
            Position position = new Position();
            position.setId(positionIds[i]);
            positions.add(position);
        }
        this.removeBatchByIds(positions);
    }

    @Override
    public void updatePosition(Integer positionId) {
        Position position = new Position();
        position.setId(positionId);
        this.removeById(position);
    }

    @Override
    public PageInfo<Position> getPosition(Position position,Integer companyId,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(positionMapper.selectPosition(position,companyId),5);
    }

    @Override
    public List<Position> getRandomEightPosition() {
        return positionMapper.selectRandomEightPosition();
    }

    @Override
    public PageInfo<Position> getTopFivePosition() {
        PageHelper.startPage(1,5);
        LambdaQueryWrapper<Position> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Position::getNumberOfResumesDelivered);
        return new PageInfo<>(this.list(lambdaQueryWrapper));
    }

    @Override
    public PageInfo<Position> selectAllPositionByCompanyId(int pageNum, int pageSize, Integer companyId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Position> positions = positionMapper.selectAllPositionByCompanyId(companyId);
        return new PageInfo<>(positions);
    }

    @Override
    public Position getPositionById(Integer id) {
        return positionMapper.selectPositionById(id);
    }

    @Override
    public List<Position> getFourPositionByPositionTypeSecondId(Integer id) {
        return positionMapper.selectFourPositionByPositionTypeSecondId(id);
    }

    @Override
    public Integer getPutOnPositionCountById(Integer id) {
        LambdaQueryWrapper<Position> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Position::getCompanyId,id);
        lambdaQueryWrapper.eq(Position::getHavePutOn,true);
        lambdaQueryWrapper.eq(Position::getEnable,true);
        return (int)this.count(lambdaQueryWrapper);
    }

    @Override
    public BigDecimal getUserCountById(Integer id) {
        QueryWrapper<Position> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sum(number_of_resumes_delivered) as userScore");
        queryWrapper.eq("company_id",id);
        Map<String, Object> map = this.getMap(queryWrapper);
        return (BigDecimal) map.get("userScore");
    }

    @Override
    public PageInfo<Position> selectAllNotEnablePositions(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(positionMapper.selectAllNotEnablePositions());
    }

    @Override
    @ManageLogAnnotation(module = "职位审核",info="审批通过了一条职位")
    public void updateEnablePosition(Integer id) {
        positionMapper.updateEnablePosition(id);
    }

    @Override
    @ManageLogAnnotation(module = "职位审核",info="拒绝通过了一条职位")
    public void deleteOnePosition(Integer id) {
        positionMapper.deleteOnePosition(id);
    }

    @Override
    public PageInfo<Position> getPositionByThings(Position position, City city, Integer positionTypeId, Integer[] workExperienceTypeIds, Integer[] educationIds, Integer[] salaryIds, Integer about, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(positionMapper.selectPositionByThings(position,city,positionTypeId,workExperienceTypeIds,educationIds,salaryIds,about),5);
    }


}
